﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class SysManage_Others_ContractNoRegulation : INTPage
{
    SqlHelp help = new SqlHelp();
    public int rowNo = 0;
    CommonMethod comHelp = new CommonMethod();

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        if (!IsPostBack)
        { WebsBind(Session["UserCode"].ToString()); }
    }

    #region 初始化绑定列表数据 WebsBind(string UserCode)
    protected void WebsBind(string UserCode)
    {
        // 页面权限
        comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "ContractNoRegulation" },
            Grv, new string[] { "cmdAdd", "cmdEdit" }, new int[] { Grv.Columns.Count - 2, Grv.Columns.Count - 1 }, CommonModel.loginOut2);
        // 数据绑定
        QueryBind(0);
        CoDepBind();
    }
    #endregion

    protected void CoDepBind()
    {
        string sql = "select distinct CoDepCode,CoDepName from B_CoDep where DeleteFlag=0 ";
        ddlCoDep.DataSource = help.GetDataTable(sql);
        ddlCoDep.DataValueField = "CoDepCode";
        ddlCoDep.DataTextField = "CoDepName";
        ddlCoDep.DataBind();
        ddlCoDep.Items.Insert(0, new ListItem("请选择(Select Please)", "-1"));
    }

    #region GridView 行事件
    // GridView换页
    protected void GrvColumns_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            Grv.PageIndex = e.NewPageIndex;
            QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
        }
        catch
        { }
    }
    //行删除
    protected void GrvColumns_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl2);

        string StrDelete = string.Format(" Delete from B_ContractNoRegulation where CNRSegment='{0}' and CNRCoDe='{1}'",
            Grv.Rows[e.RowIndex].Cells[1].Text, Grv.Rows[e.RowIndex].Cells[2].Text);
        if (help.RunSqlInt(StrDelete) > 0)
        {
            CommonMethod.regAlertMessage("删除成功！", this);
            QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
        }
        else
        {
            CommonMethod.regAlertMessage("此不存在！", this);
        }
    }
    //行编辑
    protected void GrvColumns_SelectedIndexChanged(object sender, EventArgs e)
    {
        hidEdit.Value = Grv.DataKeys[Grv.SelectedIndex]["B_ContractNoRegulationID"].ToString();
        txtSegment.Text = Grv.Rows[Grv.SelectedIndex].Cells[1].Text.Trim().ToString();

        string temp = Grv.DataKeys[Grv.SelectedIndex]["CNRCoDe"].ToString();
        ddlCoDep.SelectedIndex = 0;
        for (int i = 0; i < ddlCoDep.Items.Count; i++)
        {
            if (ddlCoDep.Items[i].Value.ToString().Equals(temp))
            {
                ddlCoDep.SelectedIndex = i;
                break;
            }
        }

        txtHead.Text = Grv.Rows[Grv.SelectedIndex].Cells[3].Text.Trim().ToString();
        txtFoot.Text = Grv.Rows[Grv.SelectedIndex].Cells[4].Text.Trim().ToString();
        txtDesc.Text = Grv.Rows[Grv.SelectedIndex].Cells[5].Text.Trim().ToString();
        
        cmdAdd.Enabled = false;
        cmdEdit.Enabled = true;
    }
    //Gridview 行事件
    protected void GrvColumns_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        rowNo++;
        //鼠标经过事件
        if (e.Row.RowIndex != -1)
        {
            //设置悬浮鼠标指针形状为"小手"
            e.Row.Attributes["style"] = "Cursor:hand";
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "lastColor=this.style.backgroundColor;this.style.backgroundColor='#CCCC99';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=lastColor;");
                ((LinkButton)e.Row.Cells[Grv.Columns.Count - 1].Controls[0]).Attributes.Add("onclick", "return confirm('确实要删除？');");
            }
        }
    }
    #endregion

    #region 查询
    protected void cmdQuery_Click(object sender, EventArgs e)
    {
        QueryBind(1);
        HidQueryFlag.Value = "1";
    }
    //
    protected void QueryBind(int n)
    {
        string StrSql = " select distinct B_ContractNoRegulationID, CNRSegment, CNRCoDe, CNRHead, CNRFoot, CNRDesc,CoDepName "
            + " from B_ContractNoRegulation CNR left join B_CoDep CD on CNR.CNRCoDe=CD.CoDepCode where CNR.DeleteFlag<>1 ";

        if (n == 1)
        {
            if (!string.IsNullOrEmpty(txtSegment.Text.ToString()))
            {
                StrSql += string.Format(" and CNRSegment like '%{0}%'", txtSegment.Text.ToString());
            }
            if (!ddlCoDep.SelectedValue.ToString().Equals("-1"))
            {
                StrSql += string.Format(" and CNRCoDe like '%{0}%'", ddlCoDep.SelectedValue.ToString());
            }
            if (!string.IsNullOrEmpty(txtDesc.Text.ToString()))
            {
                StrSql += string.Format(" and CNRDesc like '%{0}%'", txtDesc.Text.ToString());
            }
            if (!string.IsNullOrEmpty(txtHead.Text.ToString()))
            {
                StrSql += string.Format(" and CNRHead like '%{0}%'", txtHead.Text.ToString());
            }
            if (!string.IsNullOrEmpty(txtFoot.Text.ToString()))
            {
                StrSql += string.Format(" and CNRFoot like '%{0}%'", txtFoot.Text.ToString());
            }
        }

        StrSql += " order by CNRSegment,CNRCoDe ";

        DataTable Dt = help.GetDataTable(StrSql);
        Grv.DataSource = Dt;
        Grv.DataBind();
        // 显示页码信息
        if (Dt.Rows.Count > 0)
        {
            // 设置分页行永远显示
            Grv.BottomPagerRow.Visible = true;
            //赋值Label记录条数
            Label label1 = Grv.BottomPagerRow.FindControl("lblCount") as Label;
            label1.Text = Dt.Rows.Count.ToString();
        }
    }
    #endregion

    #region 添加
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        //统计列表中与要添加的类型相同的个数
        int ComN = 0;
        //
        string StrGvNSql = string.Format("select count(*) from B_ContractNoRegulation where CNRSegment='{0}' and CNRCoDe='{1}'",
            txtSegment.Text, ddlCoDep.SelectedValue.ToString());
        ComN = Convert.ToInt16(help.RunSqlReturn(StrGvNSql));
        if (ComN < 1)
        {
            //获取当前用户名称
            string UserName = Session["UserCode"].ToString();
            //当前时间
            string StrDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");

            string StrInsert = string.Format("Insert Into B_ContractNoRegulation(CNRSegment, CNRCoDe, CNRHead, CNRFoot, CNRDesc,"
                + "DeleteFlag,AddAuthor,AddDate,UpdateAuthor,UpdateDate) "
                + "Values('{0}','{1}','{2}','{3}','{4}',0,'{5}','{6}','{7}','{8}')",
                txtSegment.Text.ToString(), ddlCoDep.SelectedValue.ToString(), txtHead.Text.ToString(), txtFoot.Text.ToString(), txtDesc.Text,
                UserName, StrDate, UserName, StrDate);
            //添加结果标志
            int result = help.RunSqlInt(StrInsert);
            if (result > 0)
            {
                CommonMethod.regAlertMessage("操作成功-Success!", this);
                QueryBind(0);
                HidQueryFlag.Value = "0";
                SetFlag(true);
                txtSegment.Focus();
            }
            else
            {
                CommonMethod.regAlertMessage("出现错误-Error!", this);
                ddlCoDep.Focus();
            }
        }
        else { CommonMethod.regAlertMessage("此已经存在！", this); }
    }
    #endregion

    #region 编辑
    protected void cmdEdit_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);

        string StrUpdate = string.Format(" Update B_ContractNoRegulation set CNRHead='{0}',CNRFoot='{1}',CNRDesc='{2}', "
            + " UpdateAuthor='{3}',UpdateDate='{4}',CNRSegment='{5}',CNRCoDe='{6}' where B_ContractNoRegulationID={7} ",
            txtHead.Text, txtFoot.Text, txtDesc.Text, Session["UserCode"].ToString(),
            System.DateTime.Now.ToString("yyyy-MM-dd hh:mm"), txtSegment.Text, ddlCoDep.SelectedValue.ToString(), hidEdit.Value);
        if (help.RunSqlInt(StrUpdate) > 0)
        {
            CommonMethod.regAlertMessage("更新成功！", this);
            QueryBind(0);
            HidQueryFlag.Value = "0";

            SetFlag(true);
        }
        else
        {
            CommonMethod.regAlertMessage("更新失败！", this);
        }
    }
    #endregion

    //重置
    protected void cmdClear_ServerClick(object sender, EventArgs e)
    {
        SetFlag(true);
    }
    public void SetFlag(bool flag)
    {
        cmdAdd.Enabled = flag;
        cmdEdit.Enabled = !flag;
        txtSegment.Enabled = flag;
        txtSegment.Text = "";
        
        txtDesc.Text = "";
        txtFoot.Text = "";
        txtHead.Text = "";
    }

    //跳页
    protected void GrvColumns_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "NewPage")
            {
                TextBox tb = Grv.BottomPagerRow.FindControl("txtNewPageIndex") as TextBox;
                int pageindex = Convert.ToInt32(tb.Text);
                Grv.PageIndex = pageindex - 1;
                //数据绑定
                QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
            }
        }
        catch
        {
            CommonMethod.regAlertMessage("输入不合法-Error format!", this);
        }
    }
}